package com.ruoyi.web.controller.module.event;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.event.EventPointQr;
import com.ruoyi.system.domain.module.event.EventPointQrInfo;
import com.ruoyi.system.domain.module.survey.Survey;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.event.IEventPointQrInfoService;
import com.ruoyi.system.service.module.event.IEventPointQrService;
import com.ruoyi.system.service.module.survey.ISurveyService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 地点二维码包含的事件类型分类Controller
 *
 * @author ruoyi
 * @date 2024-08-16
 */
@Validated
@Api(value = "地点二维码包含的事件类型分类控制器", tags = {"地点二维码包含的事件类型分类管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/qrInfo")
public class EventPointQrInfoController extends BaseController {

    private final IEventPointQrInfoService eventPointQrInfoService;
	private final IEventPointQrService eventPointQrService;
	private final IEstateService estateService;
	private final ISurveyService surveyService;


    /**
     * 查询地点二维码包含的事件类型分类列表
     */
    @ApiOperation("查询地点二维码包含的事件类型分类列表")
    @PreAuthorize("@ss.hasPermi('module:qrInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<EventPointQrInfo> list(@Validated EventPointQrInfo eventPointQrInfo) {
        QueryWrapper<EventPointQrInfo> qw = new QueryWrapper<>();
		if (eventPointQrInfo.getPointQrId()!=null){
			qw.eq("point_qr_id",eventPointQrInfo.getPointQrId());
		}
		if (eventPointQrInfo.getEstateId()!=null){
			qw.eq("estate_id",eventPointQrInfo.getEstateId());
		}
        TableDataInfo<EventPointQrInfo> td = PageUtils.buildDataInfo(eventPointQrInfoService.page(PageUtils.buildPage(),qw));
        List<EventPointQrInfo> list = td.getRows();
		if (list.size()>0){
			for (EventPointQrInfo info : list){
				if(info.getPointQrId()!=null){
					EventPointQr qr = eventPointQrService.findById(info.getPointQrId());
					if (qr!=null){
						info.setPointQrName(qr.getPointName());
					}
				}
				if (info.getEstateId()!=null){
					Estate estate = estateService.findById(info.getEstateId());
					if (estate!=null){
						info.setEstateName(estate.getEstateName());
					}
				}
				if(info.getCheckTemplateId()!=null){
					Survey survey = surveyService.getById(info.getCheckTemplateId());
					if (survey!=null){
						info.setCheckTemplateName(survey.getQuestionTitle());
					}
				}
				if (info.getStandardTemplateId()!=null){
					Survey survey = surveyService.getById(info.getStandardTemplateId());
					if (survey!=null){
						info.setStandardTemplateName(survey.getQuestionTitle());
					}
				}
			}
		}
        return td;
    }

	/**
	 * 查询地点二维码包含的事件类型分类列表
	 */
	@ApiOperation("查询地点二维码包含的事件类型分类列表")
	@PreAuthorize("@ss.hasPermi('module:qrInfo:list')")
	@GetMapping("/lists")
	public List<EventPointQrInfo> lists(@Validated EventPointQrInfo eventPointQrInfo) {
		QueryWrapper<EventPointQrInfo> qw = new QueryWrapper<>();
		if (eventPointQrInfo.getPointQrId()!=null){
			qw.eq("point_qr_id",eventPointQrInfo.getPointQrId());
		}
		if (eventPointQrInfo.getEstateId()!=null){
			qw.eq("estate_id",eventPointQrInfo.getEstateId());
		}
		if(eventPointQrInfo.getEventType()!=null){
			qw.eq("event_type",eventPointQrInfo.getEventType());
		}
		List<EventPointQrInfo> list = eventPointQrInfoService.list(qw);
		if (list.size()>0){
			for (EventPointQrInfo info : list){
				if(info.getPointQrId()!=null){
					EventPointQr qr = eventPointQrService.findById(info.getPointQrId());
					if (qr!=null){
						info.setPointQrName(qr.getPointName());
					}
				}
				if (info.getEstateId()!=null){
					Estate estate = estateService.findById(info.getEstateId());
					if (estate!=null){
						info.setEstateName(estate.getEstateName());
					}
				}
				if(info.getCheckTemplateId()!=null){
					Survey survey = surveyService.getById(info.getCheckTemplateId());
					if (survey!=null){
						info.setCheckTemplateName(survey.getQuestionTitle());
					}
				}
				if (info.getStandardTemplateId()!=null){
					Survey survey = surveyService.getById(info.getStandardTemplateId());
					if (survey!=null){
						info.setStandardTemplateName(survey.getQuestionTitle());
					}
				}
			}
		}
		return list;
	}

    /**
     * 导出地点二维码包含的事件类型分类列表
     */
    @ApiOperation("导出地点二维码包含的事件类型分类列表")
    @PreAuthorize("@ss.hasPermi('module:qrInfo:export')")
    @Log(title = "地点二维码包含的事件类型分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventPointQrInfo> export(@Validated EventPointQrInfo eventPointQrInfo) {
        List<EventPointQrInfo> list = eventPointQrInfoService.list();
        ExcelUtil<EventPointQrInfo> util = new ExcelUtil<EventPointQrInfo>(EventPointQrInfo.class);
        return util.exportExcel(list, "地点二维码包含的事件类型分类");
    }

    /**
     * 获取地点二维码包含的事件类型分类详细信息
     */
    @ApiOperation("获取地点二维码包含的事件类型分类详细信息")
    @PreAuthorize("@ss.hasPermi('module:qrInfo:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventPointQrInfo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EventPointQrInfo eventPointQrInfo = eventPointQrInfoService.getById(id);
        return AjaxResult.success(eventPointQrInfo);
    }

    /**
     * 新增地点二维码包含的事件类型分类
     */
    @ApiOperation("新增地点二维码包含的事件类型分类")
    @PreAuthorize("@ss.hasPermi('module:qrInfo:add')")
    @Log(title = "地点二维码包含的事件类型分类", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventPointQrInfo eventPointQrInfo) {
        return toAjax(eventPointQrInfoService.save(eventPointQrInfo) ? 1 : 0);
    }

    /**
     * 修改地点二维码包含的事件类型分类
     */
    @ApiOperation("修改地点二维码包含的事件类型分类")
    @PreAuthorize("@ss.hasPermi('module:qrInfo:edit')")
    @Log(title = "地点二维码包含的事件类型分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventPointQrInfo eventPointQrInfo) {
        return toAjax(eventPointQrInfoService.updateById(eventPointQrInfo) ? 1 : 0);
    }

    /**
     * 删除地点二维码包含的事件类型分类
     */
    @ApiOperation("删除地点二维码包含的事件类型分类")
    @PreAuthorize("@ss.hasPermi('module:qrInfo:remove')")
    @Log(title = "地点二维码包含的事件类型分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(eventPointQrInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
